<template>
  <el-container class="home-container">
    <!--头部区域-->
    <el-header>
      <div>电商管理系统</div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>

    <el-container>
      <!--菜单区域-->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <!--菜单折叠-->
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409EFF"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="navActice"
        >
          <!--一级菜单区域-->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menuList"
            :key="item.id"
          >
            <!--一级菜单模板区域-->
            <template slot="title">
              <i :class="iconObj[item.id]"></i>
              <span slot="title">{{ item.authName }}</span>
            </template>
            <!--二级菜单区域-->
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="setNavActice('/' + subItem.path)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span slot="title">{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!--主题区域-->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      // 左侧菜单数据
      menuList: [],
      // 左侧菜单图标
      iconObj: {
        125: 'el-icon-s-custom',
        103: 'el-icon-s-flag',
        101: 'el-icon-s-goods',
        102: 'el-icon-s-order',
        145: 'el-icon-s-marketing'
      },
      // 判断菜单是否展开
      isCollapse: false,
      navActice: ''
    }
  },
  created() {
    this.getMuneList()
    this.navActice = window.sessionStorage.getItem('pathName')
  },
  methods: {
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMuneList() {
      // 获取导航栏数据
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menuList = res.data
    },
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    setNavActice(el) {
      window.sessionStorage.setItem('pathName', el)
      this.navActice = window.sessionStorage.getItem('pathName')
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #fff;
  font-size: 24px;
}
.el-aside {
  background-color: #333744;
  .el-menu,
  .el-menu-item {
    border-right: none;
  }
  .el-submenu i {
    margin-right: 10px;
  }
  .toggle-button {
    background-color: #4a5064;
    font-size: 10px;
    line-height: 24px;
    color: #fff;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
  }
}
.el-main {
  background-color: #eaedf1;
}
</style>
